﻿using Castle.Core.Configuration;
using ClosedXML.Excel;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using netcore_comm.Redis;
using netcore_service.Dto;
using SqlSugar;
using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;

namespace export
{
    public class Program
    {
        //RedisHelper定义
        private static RedisHelper _redisHelper;

        // 静态构造函数中注入
        static Program()
        {
            // 获取redis实例，注入IConfiguration
            IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
            _redisHelper = new RedisHelper(configuration);

        }
        static async Task Main(string[] args)
        {
            await ExportExcel();
        }

        static async Task ExportExcel()
        {
            try
            {

                Console.WriteLine("Hello, World!");
                var list = await _redisHelper.ListRangeAsync<SchoolInfoDto>("school_count");
                await Console.Out.WriteLineAsync("数量" + list.Count());

                var group = list.Where(e => e != null && e.Name != null).ToList();

                await Console.Out.WriteLineAsync("group" + group.Count());

                var g = group.GroupBy(e => e.Name).Select(e => new { e.Key, Count = e.Count() }).ToList();

                // 将 group 的数据写入到excel
                //// 1. 创建一个excel文件
                //var workbook = new XLWorkbook();
                //var worksheet = workbook.Worksheets.Add("School");
                //// 2. 写入数据
                //worksheet.Cell(1, 1).Value = "院校名称";
                //worksheet.Cell(1, 2).Value = "院校代码";
                //worksheet.Cell(1, 3).Value = "市区";
                //worksheet.Cell(1, 4).Value = "办学层次";
                //worksheet.Cell(1, 5).Value = "考试形式";
                //worksheet.Cell(1, 6).Value = "访问时间";

                //for (int i = 0; i < group.Count(); i++)
                //{
                //    worksheet.Cell(i + 2, 1).Value = group[i]?.Name;
                //    worksheet.Cell(i + 2, 2).Value = group[i]?.SchoolCode;
                //    worksheet.Cell(i + 2, 3).Value = group[i]?.City;
                //    worksheet.Cell(i + 2, 4).Value = group[i]?.SchoolYxcc;
                //    worksheet.Cell(i + 2, 5).Value = group[i]?.Schoolksxs;
                //    worksheet.Cell(i + 2, 6).Value = group[i]?.scDate;
                //}
                //// 3. 保存文件
                //workbook.SaveAs("School.xlsx");


                // 将group的数据写入到excel
                // 1. 创建一个excel文件
                var workbook = new XLWorkbook();
                var worksheet = workbook.Worksheets.Add("SchoolCount");
                // 2. 写入数据
                worksheet.Cell(1, 1).Value = "学校名称";
                worksheet.Cell(1, 2).Value = "学校数量";
                for (int i = 0; i < g.Count; i++)
                {
                    worksheet.Cell(i + 2, 1).Value = g[i]?.Key;
                    worksheet.Cell(i + 2, 2).Value = g[i]?.Count;
                }
                // 3. 保存文件
                workbook.SaveAs("SchoolCount.xlsx");
                // await等待输入
                Console.WriteLine("导出成功");
                Console.ReadKey();

            }
            catch (Exception ex)
            {
                await Console.Out.WriteLineAsync(ex.ToString());

                Console.ReadKey();
            }
        }
    }
}
